package com.tusuo.stds.ui.integrity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.CompleteDetail;

import java.util.ArrayList;

/**
 * Created by developer on 17-9-11.
 */

@SuppressLint("NewApi")
public class IntegrityDetailAdapter extends BaseAdapter {

    private ArrayList<CompleteDetail.ChapterRecord> mDataList;
    private Context mContext;

    public IntegrityDetailAdapter(Context context,ArrayList<CompleteDetail.ChapterRecord> chapterRecordList) {
        mContext = context;
        mDataList = chapterRecordList;
    }


    @Override
    public int getCount() {
        if (mDataList != null) {
            return mDataList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;

        if (convertView == null) {
            view = View.inflate(mContext, R.layout.item_integrity_detail_list,null);
            holder = new ViewHolder();
            holder.layoutBackground = (LinearLayout) view.findViewById(R.id.item_integrity_detail_layout_background);
            holder.layoutObject = (LinearLayout) view.findViewById(R.id.item_integrity_detail_layout_object);
            holder.tvDescriber = (TextView) view.findViewById(R.id.item_integrity_detail_tv_describer);
            holder.tvObject = (TextView) view.findViewById(R.id.item_integrity_detail_tv_object);
            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        CompleteDetail.ChapterRecord record = mDataList.get(position);

        switch (record.Type) {
            case 1:
                // 合格
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_qualified);
                break;
            case 2:
                // 模糊
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_blurry);
                break;
            case 3:
                // 问题
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_question);
                break;
            default:
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_blurry);
                break;
        }

        if (TextUtils.isEmpty(record.ObjectList) || record.ObjectList.contains("*")) {
            holder.layoutObject.setVisibility(View.GONE);
        }else {
            holder.tvObject.setText(record.ObjectList);
        }

        holder.tvDescriber.setText(record.Title);
        return view;
    }

    class ViewHolder {
        LinearLayout layoutBackground;
        LinearLayout layoutObject;
        TextView tvDescriber;
        TextView tvObject;
    }

}
